package com.gold.controller;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.gold.entity.AdminUser;
import com.gold.entity.Game;
import com.gold.entity.GameType;
import com.gold.entity.Status;
import com.gold.myexception.Myexception;
import com.gold.service.GameService;
import com.gold.service.GameTypeService;
import com.gold.service.StatusService;
import com.gold.util.Myutil;

@Controller
public class GameController {

	@Autowired
	private GameService gs;
	@Autowired
	private StatusService ss;
	@Autowired
	private GameTypeService gts;

	@RequestMapping("getAllGameByPage")
	public String getAllGameByPage(String nowPageString, ModelMap mm, HttpServletRequest request) {
		AdminUser oldau = (AdminUser) request.getSession().getAttribute("adminLoginUser");
		if (oldau == null) {
			return "redirect:gold/index.jsp";
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("nowPageString", nowPageString);
		List<Game> gameList = gs.selectAllGameByPage(map);
		List<Status> gameStatusList = ss.selectAllGameStatus();
		List<GameType> gameTypeList = gts.selectAllGameTypeByPortal();
		mm.addAttribute("gameList", gameList);
		mm.addAttribute("gameTypeList", gameTypeList);
		mm.addAttribute("gameStatusList", gameStatusList);
		mm.addAttribute("nowPage", map.get("nowPageString"));
		return "/gold/game/game.jsp";
	}

	@RequestMapping("insertGame")
	public String insertGame(@RequestParam("gameCoverFile") CommonsMultipartFile coverFile, @RequestParam("gameScreen1") CommonsMultipartFile screenFile1,
			@RequestParam("gameScreen2") CommonsMultipartFile screenFile2, @RequestParam("gameScreen3") CommonsMultipartFile screenFile3, String gameTariffeString, String gameCurrencyString,
			Game game, ModelMap mm) {
		if (Myutil.checkFileNull(coverFile, screenFile1, screenFile2, screenFile3)) {
			if (Myutil.checkStringNull(game.getGameName())) {
				if (Myutil.checkDoubleIsDouble(gameCurrencyString, gameTariffeString)) {
					String coverFileName = coverFile.getOriginalFilename();
					String screenFile1Name = screenFile1.getOriginalFilename();
					String screenFile2Name = screenFile2.getOriginalFilename();
					String screenFile3Name = screenFile3.getOriginalFilename();
					String coverFileNewName = Myutil.getDateString() + coverFileName.substring(coverFileName.lastIndexOf("."));
					String screenFile1NewName = "1-" + Myutil.getDateString() + screenFile1Name.substring(screenFile1Name.lastIndexOf("."));
					String screenFile2NewName = "2-" + Myutil.getDateString() + screenFile2Name.substring(screenFile2Name.lastIndexOf("."));
					String screenFile3NewName = "3-" + Myutil.getDateString() + screenFile3Name.substring(screenFile3Name.lastIndexOf("."));
					String coverFilePath = "/home/soft01/upload/game/";
					String screenFilePath = "/home/soft01/upload/game/screen/";
					try {
						coverFile.transferTo(new File(coverFilePath + coverFileNewName));
						screenFile1.transferTo(new File(screenFilePath + screenFile1NewName));
						screenFile2.transferTo(new File(screenFilePath + screenFile2NewName));
						screenFile3.transferTo(new File(screenFilePath + screenFile3NewName));
					} catch (IllegalStateException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					game.setGameCover(coverFileNewName);
					game.setGameScreen(screenFile1NewName + "*" + screenFile2NewName + "*" + screenFile3NewName);
					game.setGameTariffe(Double.parseDouble(gameTariffeString));
					game.setGameCurrency(Double.parseDouble(gameCurrencyString));
					try {
						System.err.println(game);
						gs.insertGame(game);
					} catch (Myexception e) {
						mm.addAttribute("msg", e.getMessage());
					}
				} else {
					mm.addAttribute("msg", "添加失败：价格类型错误");
				}
			} else {
				mm.addAttribute("msg", "添加失败：空数据");
			}
		} else {
			mm.addAttribute("msg", "添加失败：文件错误");
		}
		return "getAllGameByPage.do";
	}

	@RequestMapping("deleteGame")
	public String deleteGame(String[] gameIdStringArr) {
		if (gameIdStringArr != null && gameIdStringArr.length > 0) {
			gs.deleteGame(gameIdStringArr);
		}
		return "getAllGameByPage.do";
	}

	@RequestMapping("getGameDetails")
	public String getGameDetails(int gameId, ModelMap mm) {
		Game game = gs.selectGameByGameId(gameId);
		mm.addAttribute("gameDetails", game);
		String gameScreen = game.getGameScreen();
		String[] gameScreenArr = gameScreen.split("[*]");
		mm.addAttribute("gameScreen1", gameScreenArr[0]);
		mm.addAttribute("gameScreen2", gameScreenArr[1]);
		mm.addAttribute("gameScreen3", gameScreenArr[2]);
		return "/gold/game/gameDetails.jsp";
	}

	@RequestMapping("toUpdateGameJsp")
	public String toUpdateGameJsp(int gameId, ModelMap mm) {
		Game game = gs.selectGameByGameId(gameId);
		mm.addAttribute("gameDetails", game);
		String gameScreen = game.getGameScreen();
		String[] gameScreenArr = gameScreen.split("[*]");
		List<Status> gameStatusList = ss.selectAllGameStatus();
		List<GameType> gameTypeList = gts.selectAllGameTypeByPortal();
		mm.addAttribute("gameTypeList", gameTypeList);
		mm.addAttribute("gameStatusList", gameStatusList);
		mm.addAttribute("gameScreen1", gameScreenArr[0]);
		mm.addAttribute("gameScreen2", gameScreenArr[1]);
		mm.addAttribute("gameScreen3", gameScreenArr[2]);
		return "/gold/game/updateGame.jsp";
	}

	@RequestMapping("updateGame")
	public String updateGame(@RequestParam("gameCoverFile") CommonsMultipartFile coverFile, @RequestParam("gameScreen1") CommonsMultipartFile screenFile1,
			@RequestParam("gameScreen2") CommonsMultipartFile screenFile2, @RequestParam("gameScreen3") CommonsMultipartFile screenFile3, String gameTariffeString, String gameCurrencyString,
			Game game, ModelMap mm) {
		if (Myutil.checkDoubleIsDouble(gameCurrencyString, gameTariffeString)) {
			game.setGameCurrency(Double.parseDouble(gameCurrencyString));
			game.setGameTariffe(Double.parseDouble(gameTariffeString));
			try {
				gs.updateGame(game, coverFile, screenFile1, screenFile2, screenFile3);
			} catch (Myexception e) {
				mm.addAttribute("msg", e.getMessage());
			}
		} else {
			mm.addAttribute("msg", "修改失败：类型错误");
		}
		System.err.println(game);
		return "getAllGameByPage.do";
	}

	@RequestMapping("getGameByLike")
	public String getGameByLike(String gameName, String gameTypeId, ModelMap mm) {
		Map<String, String> map = new HashMap<String, String>();
		map.put("gameName", gameName);
		map.put("gameTypeId", gameTypeId);
		List<Game> gameList = gs.selectGameByLike(map);
		List<Status> gameStatusList = ss.selectAllGameStatus();
		List<GameType> gameTypeList = gts.selectAllGameTypeByPortal();
		mm.addAttribute("gameList", gameList);
		mm.addAttribute("gameTypeList", gameTypeList);
		mm.addAttribute("gameStatusList", gameStatusList);
		return "/gold/game/game.jsp";
	}
}
